﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;
using ForkCarRelationManager.Business;
using ForkCarRelationManager.Utility;
using common;
using ForkCarRelationManager.DB;
namespace ForkCarRelationManager.Controls
{
    public partial class MaintorControl : UserControl
    {
        MaintenBI maintenBI = new MaintenBI();

        Util util = new Util();
        MaintorBI maintorBi = new MaintorBI();
        DataPaging MaintorPage;
        DataSet MaintorDataSet;
        dbOperate dbo = new dbOperate();
        int pagecount = 18;
        bool ifedit = false;
        public MaintorControl()
        {
            InitializeComponent();
        }

    

        private void MaintorControl_Load(object sender, EventArgs e)
        {
            if (this.Btn_search.Image != null)
            {
                Btn_search.Image = new Bitmap(Btn_search.Image, Btn_search.Height - 4, Btn_search.Height - 4);
            }
            TBmaintorname.ReadOnly = false;
            TBmaintorage.ReadOnly = false;
            TBmaintorphone.ReadOnly = false;
           // textBoxX1.ReadOnly = true;
           // textBoxX1.Hide();
           // TBmaintorScom.Show();
            TBmaintorremark.ReadOnly = false;
           
            DataTable companyTable = maintenBI.Getallscompany();
            DataRow companydr = companyTable.NewRow();
            companydr[0] = "0";
            companydr[1] = "";
            companyTable.Rows.InsertAt(companydr, 0);
            TBscompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBscompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBscompany.DataSource = companyTable;
            this.TBscompany.DisplayMember = "ScompanyName";//客户所看到的
            this.TBscompany.ValueMember = "ID";//标识

            MaintorPage = maintorBi.GetMaintenpage(pagecount);
            InitData();
        }
        private void InitData()
        {
          //  MaintorPage = maintorBi.GetMaintenpage(pagecount);
            sexman.Checked = true;
           // TBmaintornum.ReadOnly = false;
            MaintorDataSet = MaintorPage.Homepage();
         //   int x = maintorBi.GetDatacount(MaintenPage);
            label1.Text = "总共" + maintorBi.GetDatacount(MaintorPage) + "条记录，当前第" + MaintorPage.GetPage() + "页，每页" + pagecount + "条";
           // MaintorDatagridView.DataSource = null;
          
            string com=TBmaintorScom.Text;
            MaintorDatagridView.DataSource = MaintorDataSet;
            MaintorDatagridView.DataMember = "data";
            DataTable companyTable = maintorBi.Getallscompany();
           /* for(int j=0;j<MaintorDatagridView.Rows.Count<j++)
            {
            for (int i = 1; i < 10; i++)
            {
                this.MaintorDatagridView.Rows[] ["Column" + i].DisplayIndex = i;
            }
            }*/
            TBmaintorScom.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBmaintorScom.DataSource = companyTable;
            this.TBmaintorScom.DisplayMember = "ScompanyName";//客户所看到的
            this.TBmaintorScom.ValueMember = "ID";//标识
            TBmaintorScom.Text = com;
        }

        private void Inforsave_Click(object sender, EventArgs e)
        {
            
            if (MaintorDatagridView.CurrentRow != null && ifedit)
            {

                string id = MaintorDatagridView.CurrentRow.Cells[(MaintorDatagridView.CurrentRow.Cells.Count) - 1].Value.ToString();
                string maintorname = TBmaintorname.Text;
                string maintorage = TBmaintorage.Text;
                string maintorphone = TBmaintorphone.Text;
                string maintorremark = TBmaintorremark.Text;
                string maintornum = TBmaintornum.Text.Trim();
                String partment = TBPartment.Text.Trim();
                String position = TBPosition.Text.Trim();
                string maintorsex;
                if (sexman.Checked)
                {
                    maintorsex = "男";
                }
                else
                    maintorsex = "女";
                if (maintorBi.Updatemaintor(maintorname, Convert.ToInt32(maintorage),Convert.ToInt32( TBmaintorScom.SelectedValue.ToString()),maintornum, maintorphone, maintorremark, maintorsex, Convert.ToInt32(id),partment,position))
                {
                    InitData();
                    MessageBox.Show("修改成功!");
                }
                else
                    MessageBox.Show("修改失败!");
            }
            else if ( !ifedit)
            {
                
                string maintorname = TBmaintorname.Text.Trim();
                string maintorage = TBmaintorage.Text.Trim();
                string maintorsex;
                if (sexman.Checked)
                {
                    maintorsex = "男";
                }
                else
                    maintorsex = "女";
                
                string maintornum = TBmaintornum.Text.Trim();
                string maintorphone = TBmaintorphone.Text.Trim();
                int scompanyid = Convert.ToInt32(TBmaintorScom.SelectedValue.ToString());
                string remark = TBmaintorremark.Text.Trim();
                String partment = TBPartment.Text.Trim();
                String position = TBPosition.Text.Trim();
                if (util.isnumeric(maintorage) && maintorage.Trim() != "" && maintornum!="")
                {
                    if (maintorBi.Addmaintor(maintorname, Convert.ToInt32(maintorage), scompanyid, maintorsex, maintornum, maintorphone, remark, partment,position))
                    {
                        MaintorPage = maintorBi.GetMaintenpage(pagecount);
                        InitData();
                        MessageBox.Show("添加成功");
                    }
                    else
                        MessageBox.Show("添加失败!");
                }
                else
                    MessageBox.Show("年龄只能为整数,姓名,身份证号不能为空");

            }
        }

        private void Btn_addmrecord_Click(object sender, EventArgs e)
        {
            Addmodal();
            
            
        }
        private void Addmodal()
        {
            ifedit = false;
            groupPanel1.Text = "添加模式";
            TBmaintorname.Text = "";
            TBmaintorage.Text = "";
            sexman.Checked = true;
            TBmaintornum.Text = "";
            TBmaintorphone.Text = "";
           // textBoxX1.Hide();
            TBmaintorScom.Show();
            TBmaintorremark.Text = "";
           // TBmaintornum.ReadOnly = false;
           // TBmaintornum.BackColor = Color.White;
            TBPartment.Text = "";
            TBPosition.Text = "";
        }

        private void btn_mainpage_Click(object sender, EventArgs e)
        {
            InitData();
        }

        private void btn_lastpage_Click(object sender, EventArgs e)
        {
            if (MaintorPage.GetPage() > 1)
            {
                MaintorDataSet = MaintorPage.GetForeData();
                MaintorDatagridView.DataSource = MaintorDataSet;
                label1.Text = "总共" + maintorBi.GetDatacount(MaintorPage) + "条记录，当前第" + MaintorPage.GetPage() + "页，每页" + pagecount + "条";
                MaintorDatagridView.DataMember = "data";
            }
            else
            {
                MessageBox.Show("已是第一页");
            }
        }

        private void btn_nextpage_Click(object sender, EventArgs e)
        {
            if (MaintorPage.GetPage() < MaintorPage.GetTotalCount())
            {
                MaintorDataSet = MaintorPage.GetFollowData();
                MaintorDatagridView.DataSource = MaintorDataSet;
                label1.Text = "总共" + maintorBi.GetDatacount(MaintorPage) + "条记录，当前第" + MaintorPage.GetPage() + "页，每页" + pagecount + "条";
                MaintorDatagridView.DataMember = "data";
            }
            else
            {
                MessageBox.Show("已是最后一页");
            }
        }

        private void btn_endpage_Click(object sender, EventArgs e)
        {
            if (MaintorPage.GetTotalCount() == 1)
            {
                InitData();
            }
            else
            {
                MaintorDataSet = MaintorPage.TailPage();
                if (MaintorDataSet != null && MaintorDataSet.Tables["data"].Rows.Count > 0)
                {
                    MaintorDatagridView.DataSource = MaintorDataSet;
                    label1.Text = "总共" + maintorBi.GetDatacount(MaintorPage) + "条记录，当前第" + MaintorPage.GetPage() + "页，每页" + pagecount + "条";
                    MaintorDatagridView.DataMember = "data";
                }
            }
        }

        private void btn_skippage_Click(object sender, EventArgs e)
        {
            if (util.isnumeric(textBoxX3.Text.Trim()))
            {
                int skippage = 1;
                if (textBoxX3.Text.Trim() != "" && textBoxX3.Text != null)
                {
                    skippage = Convert.ToInt32(textBoxX3.Text.Trim());
                }
                if (skippage == 1)
                {
                    InitData();
                }
                else
                {
                    MaintorDataSet = MaintorPage.SkipPage(skippage);
                    if (MaintorDataSet != null && MaintorDataSet.Tables["data"].Rows.Count > 0)
                    {
                        MaintorDatagridView.DataSource = MaintorDataSet;
                        label1.Text = "总共" + maintorBi.GetDatacount(MaintorPage) + "条记录，当前第" + MaintorPage.GetPage() + "页，每页" + pagecount + "条";
                        MaintorDatagridView.DataMember = "data";
                    }
                }
            }
            else
                MessageBox.Show("请输入数字再进行跳页");
        }

        private void BtnquickSearch_Click(object sender, EventArgs e)
        {
            DataTable companyTable = maintenBI.Getallscompany();
            DataRow companydr = companyTable.NewRow();
            companydr[0] = "0";
            companydr[1] = "";
            companyTable.Rows.InsertAt(companydr, 0);
            TBscompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBscompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBscompany.DataSource = companyTable;
            this.TBscompany.DisplayMember = "ScompanyName";//客户所看到的
            this.TBscompany.ValueMember = "ID";//标识
            MaintorPage = maintorBi.Searchmaintor(pagecount, Searchtext.Text.Trim(), "0");
            InitData();
            Addmodal();

        }

        private void TBscompany_SelectionChangeCommitted(object sender, EventArgs e)
        {
            MaintorPage = maintorBi.Searchmaintor(pagecount, Searchtext.Text.Trim(), TBscompany.SelectedValue.ToString().Trim());
            InitData();
            Addmodal();
        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        //导出excel
        private void button5_Click(object sender, EventArgs e)
        {
            if (util.Ifpower(LoginInfo.CurrentUser.AccountName) == 1)
            {
                ExportExl exportExl = new ExportExl();
                exportExl.ExportToExcel(this.MaintorDatagridView, MaintorPage.allData(), 0);
            }
            else
                MessageBox.Show("您没有导出excel的权限!");
           
         
        }

        private void MaintorDatagridView_Click(object sender, DataGridViewCellEventArgs e)
        {
            if (MaintorDatagridView.CurrentRow != null)
            {
                groupPanel1.Text = "编辑模式";

                //TBmaintornum.ReadOnly = true;

              //  TBmaintornum.BackColor = Color.DarkGray;
               // textBoxX1.BackColor = Color.DarkGray;
                //TBmaintorScom.Hide();
              //  textBoxX1.Show();

                ifedit = true;
                string id = MaintorDatagridView.CurrentRow.Cells[(MaintorDatagridView.CurrentRow.Cells.Count) - 1].Value.ToString();

                DataTable maintorinfor = maintorBi.Getmaintorinfor(Convert.ToInt32(id));

                TBmaintorname.Text = maintorinfor.Rows[0][0].ToString();
                TBmaintorage.Text = maintorinfor.Rows[0][1].ToString();
                if (maintorinfor.Rows[0][2].ToString() == "男")
                {
                    sexman.Checked = true;
                }
                else
                    sexwoman.Checked = true;
                TBmaintornum.Text = maintorinfor.Rows[0][3].ToString();
                TBmaintorphone.Text = maintorinfor.Rows[0][4].ToString();

                TBmaintorScom.Text = maintorinfor.Rows[0][5].ToString();
                TBmaintorremark.Text = maintorinfor.Rows[0][6].ToString();
                TBPartment.Text = maintorinfor.Rows[0]["MaintorPartment"].ToString();
                TBPosition.Text = maintorinfor.Rows[0]["MaintorPosition"].ToString();
            }
        }

       
    }
}
